package com.bhs.sansong.ui.discountCoupon;

import static com.google.android.material.tabs.TabLayout.MODE_FIXED;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.FragmentAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.CompanyCollect;
import com.bhs.sansong.bean.EnquiryOrderRefresh;
import com.bhs.sansong.bean.NumArr;
import com.bhs.sansong.bean.TabBean;
import com.bhs.sansong.ui.order.EnquiryOrderListFragment;
import com.bhs.sansong.view.SelectedFont;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * 优惠券已领取
 */
public class CouponReceivedActivity extends BaseActivity {

    private TabLayout mTabLayout;
    private ViewPager2 mViewPage;
    private List<TabBean> tabBeanList = new ArrayList<>();
    @Override
    public int initLayout() {
        return R.layout.activity_coupon_received;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("优惠券");
        mTabLayout = findViewById(R.id.tabLayout);
        mViewPage = findViewById(R.id.view_page);
        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        tabBeanList.add(new TabBean("可用券", "0"));
        tabBeanList.add(new TabBean("已使用", "1"));
        tabBeanList.add(new TabBean("过期券", "2"));
        mTabLayout.setTabMode(MODE_FIXED);
        //tab切换时候的字体设置
        mTabLayout.addOnTabSelectedListener(new SelectedFont(this));
        ArrayList<BaseFragment> fragments = new ArrayList<>();

        fragments.add(CouponReceivedFragment.newInstance(tabBeanList.get(0).getType()));
        fragments.add(CouponReceivedFragment.newInstance(tabBeanList.get(1).getType()));
        fragments.add(CouponReceivedFragment.newInstance(tabBeanList.get(2).getType()));
        mViewPage.setOffscreenPageLimit(fragments.size());
        mViewPage.setAdapter(new FragmentAdapter(this, fragments));
        new TabLayoutMediator(mTabLayout, mViewPage, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabBeanList.get(position).getTypeName());
            }
        }).attach();

    }

    @Subscribe
    public void onEvent(NumArr event) {
        mTabLayout.getTabAt(0).setText(String.format("可用券(%s)",event.unUsedNum));
        mTabLayout.getTabAt(1).setText(String.format("已使用(%s)",event.usedNum));
        mTabLayout.getTabAt(2).setText(String.format("过期券(%s)",event.passedNum));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}